<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Prison as PrisonModel;

/**
 *
 */
class Prison extends Api
{

    //如果$noNeedLogin为空表示所有接口都需要登录才能请求
    //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
    //如果接口已经设置无需登录,那也就无需鉴权了
    //
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    /**
     * 获取列表
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getLst()
    {
        $keywords = $this->request->param('keywords') ?: '';
        $province_id = $this->request->param('province_id') ?: 0;
        $city_id = $this->request->param('city_id') ?: 0;
        $type = $this->request->param('type') ?? -1;
        $list = PrisonModel::with(['province', 'city', 'area'])->where(['status' => 1])->where(function ($query) use ($keywords, $province_id, $city_id, $type) {
            if ($keywords) {
                $query->whereLike('name', '%' . $keywords . '%');
            }
            if ($province_id) {
                if (!$city_id || in_array($city_id,[1101,1201,3101,5001])) {
                    $query->where(['province_id' => $province_id]);
                } else {
                    $query->where(['city_id' => $city_id]);
                }
            }
            if ($type > -1) {
                $query->where(['type' => $type]);
            }
        })->select();
        $this->success('获取成功！', $list);
    }

}
